//
//  XZSearchBar.m
//  Teecloud
//
//  Created by PC on 2018/10/24.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZSearchBar.h"

@interface XZSearchBar ()

//placeholder 和icon 和 间隙的整体宽度
@property (nonatomic, assign) CGFloat placeholderWidth;

@end
static CGFloat const searchIconW = 20.0;
//icon与placeholder间距
static CGFloat const iconSpacing = 10.0;
//占位文字的字体大小
static CGFloat const placeHolderFont = 14.0;
@implementation XZSearchBar

- (void)layoutSubviews {
    [super layoutSubviews];
    //修改搜索按钮跟占位符的间距
    self.searchTextPositionAdjustment = UIOffsetMake(iconSpacing,0);
    
    for (UIView *view in [self.subviews lastObject].subviews) {
        
        if ([view isKindOfClass:[UITextField class]]) {
            
            UITextField *field = (UITextField *)view;
            
            // 重设field的frame
            
            field.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
            field.font = [UIFont systemFontOfSize:12.0f];
            [field setBackgroundColor:[UIColor whiteColor]];
            
            field.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
            
            
            
            field.borderStyle = UITextBorderStyleNone;
            
            field.layer.cornerRadius = 2.0f;
            
            field.layer.masksToBounds = YES;
            
            
            
            // 设置占位文字字体颜色
            
            [field setValue:[UIColor colorWithRed:156/255.0 green:156/255.0 blue:156/255.0 alpha:1] forKeyPath:@"_placeholderLabel.textColor"];
            
            [field setValue:[UIFont systemFontOfSize:placeHolderFont] forKeyPath:@"_placeholderLabel.font"];
            
            
            
            if (@available(iOS 11.0, *)) {
                
                // 先默认居中placeholder
                
                [self setPositionAdjustment:UIOffsetMake((field.frame.size.width-self.placeholderWidth)/2, 0) forSearchBarIcon:UISearchBarIconSearch];
                
            }
            
        }
        
    }
    
}


// 开始编辑的时候重置为靠左

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    // 继续传递代理方法
    
    if ([self.delegate respondsToSelector:@selector(searchBarShouldBeginEditing:)]) {
        
        [self.delegate searchBarShouldBeginEditing:self];
        
    }
    
    if (@available(iOS 11.0, *)) {
        
        [self setPositionAdjustment:UIOffsetZero forSearchBarIcon:UISearchBarIconSearch];
        
    }
    
    return YES;
    
}

// 结束编辑的时候设置为居中

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    
    if ([self.delegate respondsToSelector:@selector(searchBarShouldEndEditing:)]) {
        
        [self.delegate searchBarShouldEndEditing:self];
        
    }
    
    if (@available(iOS 11.0, *)) {
        
        [self setPositionAdjustment:UIOffsetMake((textField.frame.size.width-self.placeholderWidth)/2, 0) forSearchBarIcon:UISearchBarIconSearch];
        
    }
    
    return YES;
    
}


// 计算placeholder、icon、icon和placeholder间距的总宽度

- (CGFloat)placeholderWidth {
    
    if (!_placeholderWidth) {
        
        CGSize size = [self.placeholder boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:placeHolderFont]} context:nil].size;
        
        _placeholderWidth = size.width + iconSpacing + searchIconW;
        
    }
    
    return _placeholderWidth;
    
}


@end
